#include "Map.h"
#include "../Wrapper/CSGD_TextureManager.h"
//#include "../Wrapper/CSGD_Direct3D.h"

Map::Map(short _mapWidth, short _mapHeight, short _tileWidth, short _tileHeight)
	: mapWidth(_mapWidth), mapHeight(_mapHeight), tileWidth(_tileWidth), tileHeight(_tileHeight)
{
}

Map::~Map()
{
}

void Map::SetDimensions(short _mapWidth, short _mapHeight, short _tileWidth, short _tileHeight)
{
	mapWidth = _mapWidth;
	mapHeight = _mapHeight;
	tileWidth = _tileWidth;
	tileHeight = _tileHeight;
}

void Map::LoadTileset(const char* _fileName, short _setWidth, short _setHeight, short _tileWidth, short _tileHeight)
{
	Tileset newSet;

	newSet.texID = CSGD_TextureManager::GetInstance()->LoadTexture(_fileName);

	newSet.setWidth = _setWidth;
	newSet.setHeight = _setHeight;
	newSet.tileWidth = _tileWidth;
	newSet.tileHeight = _tileHeight;

	tileSetVec.push_back(newSet);
}

void Map::LoadTile(short _tileSetX, short _tileSetY, short _tileSetIndex)
{
	Tile newTile;

	newTile.tileSetIndex = _tileSetIndex;
	newTile.tileSetX = _tileSetX;
	newTile.tileSetY = _tileSetY;

	tileVec.push_back(newTile);
}

void Map::Render()
{
	//for (int x = 0; x < mapWidth; ++x)
	//{
	//	for (int y = 0; y < mapHeight; ++y)
	//	{
	//		if (tileVec[mapHeight * x + x + y].tileSetIndex != -1)
	//		{
	//			short sourceTileWidth = tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].tileWidth;
	//			short sourceTileHeight = tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].tileHeight;

	//			RECT sourceRect;
	//			sourceRect.left = tileVec[(mapHeight - 1) * x + x + y].tileSetX * sourceTileWidth;
	//			sourceRect.top = tileVec[(mapHeight - 1) * x + x + y].tileSetY * sourceTileHeight;
	//			sourceRect.right = sourceRect.left + sourceTileWidth;
	//			sourceRect.bottom = sourceRect.top + sourceTileHeight;

	//			CSGD_TextureManager::GetInstance()->Draw(tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].texID,
	//				(int)(x * tileWidth), (int)(y * tileHeight),
	//				tileWidth / (float)sourceTileWidth, tileHeight / (float)sourceTileHeight,
	//				&sourceRect);
	//		}
	//	}
	//}

	for (int x = 0; x < mapWidth; ++x)
	{
		for (int y = 0; y < mapHeight; ++y)
		{
				short sourceTileWidth = tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].tileWidth;
				short sourceTileHeight = tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].tileHeight;

				RECT sourceRect;
				sourceRect.left = tileVec[(mapHeight - 1) * x + x + y].tileSetX * sourceTileWidth;
				sourceRect.top = tileVec[(mapHeight - 1) * x + x + y].tileSetY * sourceTileHeight;
				sourceRect.right = sourceRect.left + sourceTileWidth;
				sourceRect.bottom = sourceRect.top + sourceTileHeight;

				CSGD_TextureManager::GetInstance()->Draw(tileSetVec[tileVec[(mapHeight - 1) * x + x + y].tileSetIndex].texID,
					(int)(x * tileWidth), (int)(y * tileHeight),
					tileWidth / (float)sourceTileWidth, tileHeight / (float)sourceTileHeight,
					&sourceRect);
		}
	}
}